package com.xiahui.day05.Test2;

import java.util.Arrays;

public class TestArrayCopy3 {
    public static void main(String[] args) {
        int[] sourceArray = {1, 2, 3, 4, 5};

        // 使用 Arrays.copyOf() 复制数组
        int[] targetArray = Arrays.copyOf(sourceArray, sourceArray.length);
        // 复制整个数组
        int[] shorterArray = Arrays.copyOf(sourceArray, 3);
        // 截断数组：{1, 2, 3}
        int[] longerArray = Arrays.copyOf(sourceArray, 7);
        // 扩展数组：{1, 2, 3, 4, 5, 0, 0}

        // 打印目标数组
        System.out.print("targetArray: ");
        for (int num : targetArray) {
            System.out.print(num + " "); // 输出：1 2 3 4 5
        }
        System.out.println();

        System.out.print("shorterArray: ");
        for (int num : shorterArray) {
            System.out.print(num + " "); // 输出：1 2 3
        }
        System.out.println();

        System.out.print("longerArray: ");
        for (int num : longerArray) {
            System.out.print(num + " "); // 输出：1 2 3 4 5 0 0
        }
        System.out.println();
    }
}
